<?php

    $login = 'gta1807838';
    $pass = '7x62zUN7kT';
    $domain = 's18.hgs.su';
    
    $conn = ftp_connect($domain);
    if (ftp_login($conn, $login, $pass)) {
        $remote_file = 'scriptfiles/online_now.txt';
        $handle = fopen('temp_players', 'w');
        
        if (!in_array($remote_file, (array)ftp_nlist($conn, dirname($remote_file))))
            exit();
        
        if (ftp_fget($conn, $handle, $remote_file, FTP_ASCII, 0)) { // (ftp_nb_get)
            fclose($handle);
            
            $players = array();
            $file = fopen('temp_players', 'r');
            $count = 0;
            while (!feof($file)) {
                $mytext = fgets($file, 999);
                $players[] = $mytext;
                $count ++;
            }
            fclose($file);
            unlink('temp_players');
            
            $result = array(
                'players' => $players,
                'count' => $count - 1
            );
            
            echo json_encode($result);
        }
    }
